import json

from DrissionPage import Chromium

driver = Chromium()

tab = driver.latest_tab

tab.listen.start('/wapi/zpCommon/data/city')

tab.get('https://www.zhipin.com/')

res = tab.listen.wait()
res = res.response.body
# print(res)
city_list = res['zpData']['cityList']
for city in city_list:
    information = {
        'detail': []
    }
    province = city['name']
    province_id = city['code']
    information['detail'].append({
        f'{province}': {},
        'province_id': province_id
    })
    level_list = city['subLevelModelList']
    if level_list:
        for level in level_list:
            level_name = level['name']
            level_id = level['code']
            information['detail'][0][f'{province}'][f'{level_name}'] = {}
            information['detail'][0][f'{province}'][f'{level_name}']['level_id'] = level_id
            region_list = level['subLevelModelList']
            if region_list:
                for region in region_list:
                    region_name = region['name']
                    region_id = region['code']
                    information['detail'][0][f'{province}'][f'{level_name}'][f'{region_name}'] = region_id
    print(information)
    with open('information.txt', 'a', encoding='utf-8') as f:
        f.write(json.dumps(information, ensure_ascii=False) + '\n')

tab.listen.start('/wapi/zpCommon/data/getCityShowPosition')

tab.get('https://www.zhipin.com/')

res = tab.listen.wait()
res = res.response.body
category_list = res['zpData']['position']
for category in category_list:
    job_information = {
        'detail': []
    }
    category_name = category['name']
    category_id = category['code']
    job_information['detail'].append({
        f'{category_name}': {},
        "category_id": category_id
    })
    job_list = category['subLevelModelList']
    if job_list:
        for job in job_list:
            job_name = job['name']
            job_id = job['code']
            job_information['detail'][0][f'{category_name}'][f'{job_name}'] = {}
            job_information['detail'][0][f'{category_name}'][f'{job_name}']['job_id'] = job_id
            major_list = job['subLevelModelList']
            if major_list:
                for major in major_list:
                    major_name = major['name']
                    major_id = major['code']
                    job_information['detail'][0][f'{category_name}'][f'{job_name}'][f'{major_name}'] = major_id
    print(job_information)
    with open('jsb_information.txt', 'a', encoding='utf-8') as f:
        f.write(json.dumps(job_information, ensure_ascii=False) + '\n')


